﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;
using LibrarySolution.ComMethod;

namespace LibrarySolution.Frm
{
    public partial class FrmKfxx : Form
    {
        #region 属性
        KfDao kfDao;
        JHBkDao jhbkDao;
        JSRecord jsEntity;
        KfxxDetail kfxxDetail;
        YhxxEntity yhxxEntity;
        DataTable jylsDt;
        YhDao yhDao;
        Double sum, yhYe;
        private Boolean isCqkf;
        public static Boolean isModify;
        private static FrmKfxx Instance;
        public static bool isCancled = false;
        #endregion

        #region 构造器
        public FrmKfxx()
        {
            InitializeComponent();
        }
        #endregion

        #region 事件
        public static FrmKfxx GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
            {
                Instance = new FrmKfxx();
                Instance.MdiParent = GlobalVar.frmMain;
                GlobalVar.frmMain.panel1.Controls.Add(Instance);
                Instance.BringToFront();
            }
            return Instance;

        }

        public static FrmKfxx GetInstance(DataTable dt)
        {
            Instance = new FrmKfxx();
            Instance.LblYhNo.Visible = false;
            Instance.TxtQueryByYhNo.Visible = false;
            Instance.BtnCqcx.Visible = false;
            BindingData(dt);
            return Instance;
        }

        private void BtnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnDone_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource == null) return;
            paymo(sum);
        }

        public static void BindingData(DataTable dt)
        {
            Instance.dataGridView1.DataSource = dt;
            Instance.dataGridView1.Refresh();
        }

        private void BtnChange_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            String yhNo = TxtQueryByYhNo.Text.Trim();
            sum = 0;
            isCqkf = true;
            yhDao = new YhDao();
            yhxxEntity = new YhxxEntity();
            yhxxEntity = yhDao.getYhxxByNo(yhNo,"1");
            if (yhxxEntity.yh_no != null && yhxxEntity.yh_code != null)
            {
                #region 属性
                Int32 num;                          // 获得借书记录的条数
                Double money = 0.0;                 // 取出应扣费用
                String days;                        // 获得超期的天数
                DataRow dataRow;                    // 用于新DataTable的行
                DataTable dataTable;                // 获得数据库内的借书记录
                jhbkDao = new JHBkDao();
                jsEntity = new JSRecord();
                jylsDt = new DataTable();           // 应扣费的借书记录的集合
                jsEntity.js_yhno = yhxxEntity.yh_no;
                #endregion

                dataTable = jhbkDao.getTsjl(jsEntity);
                num = dataTable.Rows.Count;
                setdataTable();
                for (int i = 0; i < num; i++)
                {
                    DateTime ghRq = DateTime.Parse(dataTable.Rows[i]["归还日期"].ToString());

                    if (ghRq < DateTime.Now)
                    {
                        if (dataTable.Rows[i]["是否扣费"].ToString() == "0" &&
                            dataTable.Rows[i]["图书状态"].ToString() == "在库")
                        {
                            DateTime shRq = DateTime.Parse(dataTable.Rows[i]["实还日期"].ToString());
                            TimeSpan Span = shRq.Subtract(ghRq);
                            days = Convert.ToString( Span.Days);
                            money = Convert.ToInt32(days) * 0.1;
                            sum += money;
                            dataRow = jylsDt.NewRow();
                            dataRow[0] = dataTable.Rows[i]["书本编号"].ToString();
                            dataRow[1] = dataTable.Rows[i]["书本名称"].ToString();
                            dataRow[2] = yhxxEntity.yh_no;
                            dataRow[3] = yhxxEntity.yh_name;
                            dataRow[4] = ghRq.ToString("yyyy-MM-dd");
                            dataRow[5] = days;
                            dataRow[6] = Convert.ToString(money);
                            dataRow[7] = GlobalVar.GlyEntity.gly_name;
                            dataRow[8] = DateTime.Now.ToString("yyyy-MM-dd");
                            jylsDt.Rows.Add(dataRow);
                        }
                    }
                }
                dataRow = jylsDt.NewRow();
                dataRow[0] = "合计:";
                dataRow[6] = sum;
                jylsDt.Rows.Add(dataRow);
                if (jylsDt.Rows.Count == 1)
                    MessageBox.Show("您未欠费");
                else
                    dataGridView1.DataSource = jylsDt;
            }
            else
                MessageBox.Show("用户不存在或已查封");
            TxtQueryByYhNo.Text = "";
        }

        private void FrmKfxx_FormClosing(object sender, FormClosingEventArgs e)
        {
            GlobalVar.YhxxEntity.yh_no = null;
        }

        private void BtnDscx_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            sum = 0;
            isCqkf = false;
            Double money;
            Int32 count;
            String yhNo = TxtQueryByYhNo.Text.Trim();
            yhDao = new YhDao();
            yhxxEntity = new YhxxEntity();
            yhxxEntity = yhDao.getYhxxByNo(yhNo,"1");
            if (yhxxEntity.yh_no != null && yhxxEntity.yh_code != null)
            {
                DataTable dsDataTable;
                jsEntity = new JSRecord();
                jhbkDao = new JHBkDao();
                jsEntity.js_yhno = yhNo;
                dsDataTable = jhbkDao.getDsBkJl(jsEntity);
                if (dsDataTable != null)
                {
                    count = dsDataTable.Rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        money = Convert.ToDouble(dsDataTable.Rows[i][3]);
                        sum += money;
                    }
                    DataRow addRow = dsDataTable.NewRow();
                    sum += sum;
                    addRow[0] = "总额(注:丢失应付双倍)";
                    addRow[3] = Convert.ToString(sum);
                    dsDataTable.Rows.Add(addRow);
                    if (dsDataTable.Rows.Count == 1)
                        MessageBox.Show("您未丢失图书");
                    else
                        dataGridView1.DataSource = dsDataTable;
                }
                else
                    MessageBox.Show("您未丢失图书");
            }
            else
                MessageBox.Show("用户不存在或已查封");
            TxtQueryByYhNo.Text = "";
        }
        #endregion

        #region 方法
        void paymo(Double sum)
        {
            kfDao = new KfDao();
            kfxxDetail = new KfxxDetail();
            kfxxDetail.yh_no = yhxxEntity.yh_no;
            kfxxDetail.kf_glyname = GlobalVar.GlyEntity.gly_name;
            yhYe = kfDao.getYhye(kfxxDetail);
            kfxxDetail.yh_zhye = Convert.ToString(yhYe);
            if (yhYe < sum)
            {
                MessageBox.Show("余额不足,请先充值");
                dataGridView1.DataSource = null;
                isCancled = true;
                this.Close();
                return;
            }
            kfxxDetail.kf_kfsj = DateTime.Now.ToString("yyyy-MM-dd");
            for (int i = 0; i < Instance.dataGridView1.Rows.Count - 1; i++)
            {
                kfxxDetail.bk_no = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
                if (isCqkf)
                {
                    kfxxDetail.kf_ghrq = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value);
                    kfDao.cqkf(kfxxDetail);
                }
                else
                    kfDao.dskf(kfxxDetail);
            }
            kfxxDetail.kf_kcfy = Convert.ToString(sum);
            kfDao.insertKf(kfxxDetail);
            MessageBox.Show("扣费成功!", "提示");
            this.Close();
            GlobalVar.failtopay = false;
            this.Close();
        }

        void setdataTable()
        {
            jylsDt = new DataTable();
            jylsDt.Columns.Add("图书编号");
            jylsDt.Columns.Add("图书名称");
            jylsDt.Columns.Add("用户编号");
            jylsDt.Columns.Add("用户名称");
            jylsDt.Columns.Add("归还日期");
            jylsDt.Columns.Add("超期天数");
            jylsDt.Columns.Add("扣除费用");
            jylsDt.Columns.Add("操作员名称");
            jylsDt.Columns.Add("操作日期");
        }
        #endregion

    }
}
